package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.AttachmentType;
import com.pactera.jep.sys.service.AttachmentTypeService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.code.WebHeaderCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "附件类型")
@RestController
@RequestMapping("/attachmentType")
public class AttachmentTypeRestController extends BaseRestController<AttachmentType> {
	Logger logger = LoggerFactory.getLogger(AttachmentTypeRestController.class);

	@Autowired
	private AttachmentTypeService	attachmentTypeService;

	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(@ApiIgnore @RequestHeader(WebHeaderCode.X_USER_TOKEN) String token, Page<AttachmentType> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {

		attachmentTypeService.query(page, filters, orders);
	}

	/**
	 * 保存
	 * @param attachmentType
	 * @return
	 */
	@ApiOperation(value = "保存一条数据")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody AttachmentType attachmentType) {

		Map<String, Object> tips = new HashMap<String, Object>();

		Boolean isExist = attachmentTypeService.exists(attachmentType.getAttachTypeCode());
		try
		{
			if(isExist)
			{
				attachmentTypeService.update(attachmentType);
			}
			else
			{
				attachmentTypeService.insert(attachmentType);
			}

			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e)
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}

	/**
	 * 删除
	 * @param attachTypeCode
	 * @return
	 */
	@ApiOperation(value = "根据编码删除一条数据")
	@PostMapping(value = "/remove/{attachTypeCode}")
	public Map<String, Object> remove(@PathVariable String attachTypeCode){

		Map<String, Object> tips = new HashMap<String, Object>();

		try
		{
			attachmentTypeService.deleteByPrimaryKey(attachTypeCode);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		}
		catch (ServiceException e)
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE+",请检查该附件类型是否被使用");
		}

		return tips;
	}

	/**
	 * 验证提交的attachTypeCode是否已经存在数据库中
	 * @param attachTypeCode
	 * @return
	 */
	@ApiOperation(value = "验证提交的编码是否已经存在数据库中")
	@GetMapping(value = "/check", produces = "application/json;charset=UTF-8")
	public Map<String, Object> check(String attachTypeCode){

		Map<String, Object> tips = new HashMap<String, Object>();

		Boolean exist = attachmentTypeService.exists(attachTypeCode);
		tips.put("valid", !exist);

		return tips;
	}

	@RequestMapping(value = "/get")
	public String get(String attachTypeCode){
		AttachmentType attachmentType = attachmentTypeService.get(attachTypeCode);
		if(attachmentType != null){
			return JSONObject.toJSONString(attachmentType);
		}
		return null;
	}
}
